package com.future.mayi.qsale.utils;

import android.content.Context;
import android.os.AsyncTask;
import android.text.TextUtils;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.Target;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/8/22.
 */

public class LoadImageUtils {

    private Context mContext;
    private List<PrizeImgModel> mInitList;
    private LoadImageListener mLoadImageListener;
    private List<PrizeImgModel> prizeImgModels;

    public LoadImageUtils(Context context, List<PrizeImgModel> urls) {
        mInitList = urls;
        mContext = context;
    }

    public void startDownload() {
        for (int i = 0; i < mInitList.size(); i++) {

            new LoadImageTask(mContext, mInitList.get(i).getPrizeId()).execute(mInitList.get(i).getUrls());
        }
    }

    public void loadSucceed(PrizeImgModel result) {
        if (prizeImgModels == null) prizeImgModels = new ArrayList<>();
        prizeImgModels.add(result);
        if (prizeImgModels.size() == mInitList.size()) {
            if (mLoadImageListener != null) {
                mLoadImageListener.loadSucceed(prizeImgModels);
            }
        }
    }

    class LoadImageTask extends AsyncTask<String, Void, PrizeImgModel> {
        private final Context context;
        private int prizeId;

        public LoadImageTask(Context context, int prizeId) {
            this.context = context;
            this.prizeId = prizeId;
        }

        @Override
        protected PrizeImgModel doInBackground(String... params) {
            String url = params[0];
            if (TextUtils.isEmpty(url)) return new PrizeImgModel();
            PrizeImgModel prizeImgModel = new PrizeImgModel();
            prizeImgModel.setUrls(url);
            prizeImgModel.setPrizeId(prizeId);
            try {
                String localPath = Glide.with(context)
                        .load(url)
                        .downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
                        .get().getPath();
                prizeImgModel.setLocalPath(localPath);
                return prizeImgModel;
            } catch (Exception ex) {
                return prizeImgModel;
            }
        }

        @Override
        protected void onPostExecute(PrizeImgModel result) {
            loadSucceed(result);
        }
    }

    public LoadImageUtils setLoadImageListener(LoadImageListener loadImageListener) {
        mLoadImageListener = loadImageListener;
        return this;
    }

    public interface LoadImageListener {

        void loadSucceed(List<PrizeImgModel> prizeImgModels);
    }

    public static class PrizeImgModel {
        int prizeId;
        String urls;
        String localPath;

        public int getPrizeId() {
            return prizeId;
        }

        public void setPrizeId(int prizeId) {
            this.prizeId = prizeId;
        }

        public String getUrls() {
            return urls;
        }

        public void setUrls(String urls) {
            this.urls = urls;
        }

        public String getLocalPath() {
            return localPath;
        }

        public void setLocalPath(String localPath) {
            this.localPath = localPath;
        }
    }
}
